package com.example.lending.ViewModel.Repository;

import android.content.Context;
import android.os.AsyncTask;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MediatorLiveData;
import androidx.lifecycle.Observer;

import com.example.lending.Model.Dao.UserDao;
import com.example.lending.Model.Database;
import com.example.lending.Model.User;

import java.text.DecimalFormat;
import java.util.List;

public class UserRepository {
    private LiveData<List<User>> allUserLive;
    private UserDao userDao;
    private Context context;
    private MediatorLiveData<List<User>> userLiveOb;
    LiveData<List<User>>oneUserLive;
    int x;
    public LiveData<List<User>> getAllUserLive(){return allUserLive;}
    public LiveData<List<User>> findUserWithPattern(final String pattern){
        //final LiveData<List<User>>oneUserLive= Database.getDatabase(context.getApplicationContext()).getUserDao().findUserWithPattern("%"+pattern+"%");
        /*userLiveOb.addSource(oneUserLive, new Observer<List<User>>() {
            @Override
            public void onChanged(List<User> users) {
                if(users==null||users.isEmpty()){
                    Database.getDatabase(context.getApplicationContext()).getQueryExecutor();
                }else {
                    userLiveOb.removeSource(oneUserLive);
                    userLiveOb.setValue(users);
                }
            }
        });*/

        if(oneUserLive==null)
            oneUserLive=Database.getDatabase(context.getApplicationContext()).getUserDao().findUserWithPattern("%"+pattern+"%");
        return oneUserLive;
    }
    public List<User> findUsers(String pattern){
        final LiveData<List<User>>oneUserLive= Database.getDatabase(context.getApplicationContext()).getUserDao().findUserWithPattern("%"+pattern+"%");
        userLiveOb.addSource(oneUserLive, new Observer<List<User>>() {
            @Override
            public void onChanged(List<User> users) {
                if(users==null||users.isEmpty()){

                }else {
                    userLiveOb.removeSource(oneUserLive);
                    userLiveOb.setValue(users);
                }
            }
        });
        return userDao.findUsers("%"+pattern+"%");
    }


    public UserRepository(Context context){
        Database userDatabase = Database.getDatabase(context.getApplicationContext());
        this.context=context;
        userDao=userDatabase.getUserDao();
        allUserLive = userDao.getAllUserLive();
        userLiveOb=new MediatorLiveData<List<User>>();
        x=0;
    }
    public void insertUsers(User... users){
        new UserRepository.InsertAsyncTask(userDao).execute(users);
    }
    public void updataUsers(User... users){
        new UserRepository.UpdataAsyncTask(userDao).execute(users);
    }
    public void deleteUsers(User... users){
        new UserRepository.InsertAsyncTask(userDao).execute(users);
    }
    public void deleteAllUsers(Void... voids){
        new UserRepository.DeleteAllAsyncTask(userDao).execute();
    }

    static class InsertAsyncTask extends AsyncTask<User,Void,Void> {
        private UserDao userDao;
        public InsertAsyncTask(UserDao userDao) {
            this.userDao = userDao;
        }

        @Override
        protected Void doInBackground(User... users) {
            userDao.insertUser(users);
            return null;
        }
    }
    static class UpdataAsyncTask extends AsyncTask<User,Void,Void>{
        private UserDao userDao;
        public UpdataAsyncTask(UserDao userDao) {
            this.userDao = userDao;
        }
        @Override
        protected Void doInBackground(User... users) {
            userDao.updataUser(users);
            return null;
        }
    }
    static class DeleteAsyncTask extends AsyncTask<User,Void,Void>{
        private UserDao userDao;
        public DeleteAsyncTask(UserDao userDao) {
            this.userDao = userDao;
        }

        @Override
        protected Void doInBackground(User... users) {
            userDao.deleteUser(users);
            return null;
        }
    }
    static class DeleteAllAsyncTask extends AsyncTask<Void,Void,Void>{
        private UserDao userDao;
        public DeleteAllAsyncTask(UserDao userDao) {
            this.userDao = userDao;
        }

        @Override
        protected Void doInBackground(Void... voids) {
            userDao.deleteAllUser();
            return null;
        }
    }

    static class FindUsersAsyncTask extends AsyncTask<Void,Void,Void>{
        private UserDao userDao;
        public FindUsersAsyncTask(UserDao userDao) {
            this.userDao = userDao;
        }

        @Override
        protected Void doInBackground(Void... voids) {
            return null;
        }
    }


}
